import numpy as np

def cal_time(list1,list2):
    new_list=[]

    # list2_np=np.array(list2)
    # list2_np=list2_np[:,0]
    # list2_row=list2_np.tolist()

    # print(list2_row)

    # for i in list1:
    #     list1_begin=i[0]
    #     list1_end=i[1]
    #     for list2_begin in list2_row:
    #         if list1_begin<=list2_begin<=list1_end:
    #             j=list2_row.index()
    #             list2_end=list2[j][1]
    #             new_end=max(list2_end,list1_end)
    #             new_begin=list1_begin

    while list1 and list2:
        list1_begin=list1[0][0]
        list1_end=list1[0][1]

        list2_begin=list2[0][0]
        list2_end=list2[0][1]

        print(list1_begin,list1_end,list2_begin,list2_end)

        # list1_begin<list1_end<list2_begin<list2_end
        if list2_begin>list1_end:
            new_list.append(list1[0])
            del list1[0]
        else:
            # list1_begin<list2_begin<=list2_end<=list1_end
            if list2_end<=list1_end:
                new_list.append(list1[0])
                del list1[0]
                del list2[0]
            else:
                # list1_begin<list2_begin<=list1_end<list2_end
                # list2_end>list1
                if list1[list1.__len__()-1][1]<=list2_end:
                    new_list.append([list1_begin,list2_end])
                    del list1[:]
                    del list2[0]
                else:
                    for line in list1:
                        for row in line:
                            if list2_end<=row:
                                row_index=line.index(row)
                                line_index=list1.index(line)
                                if row_index==0:
                                    new_list.append([list1_begin,list2_end])
                                    del list1[:line_index]
                                    del list2[0]
                                elif row_index==1:
                                    new_list.append([list1_begin,list1[line_index][row_index]])
                                    del list1[:line_index+1]
                                    del list2[0]
                                else:
                                    print("row index error")
                                
                                break
                
    if list1:
        new_list.extend(list1)
    if list2:
        new_list.extend(list2)

    print(str(new_list))



list1=[[1,3],[4,6]]
list2=[[2,5],[7,8]]
cal_time(list1,list2)
